package com.kly.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.api.Page;
import com.fqgj.common.api.Response;
import com.google.common.collect.Maps;
import com.kly.user.chatterService.ChatterUserService;
import com.kly.user.db.dao.UserDao;
import com.kly.user.db.dao.UserTaskDetailDao;
import com.kly.user.db.entity.UserTaskDetailEntity;
import com.kly.user.db.entity.UserTaskJobEntity;
import com.kly.user.dto.*;
import com.kly.user.dto.chatter.response.ChatterDashboardDto;
import com.kly.user.enums.AppEnum;
import com.kly.user.enums.LikeEnum;
import com.kly.user.enums.PushBizCodeEnum;
import com.kly.user.msgClient.MsgAPI;
import com.kly.user.service.*;
import com.kly.user.task.ExecuteResult;
import com.kly.user.task.IssueUserPondTask;
import com.kly.user.task.OnlineStatusCheckTask;
import com.kly.user.task.enums.TaskTypeEnum;
import com.kly.user.task.service.StatisticalImpl;
import com.kly.user.task.v2.RemoveDataTask;
import com.kly.user.task.v2.SendFlashChatMsgTask;
import com.kly.user.task.v2.appflyers.AppflyerPullApiTask;
import com.kly.user.task.v2.MessageStatusStatisticalTask;
import com.kly.user.task.v2.appflyers.PushToAppFlyerRetryTask;
import com.kly.user.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: yujianfu (mr.vencnet@gmail.com)
 * Date: 2019-11-25
 * Time: 11:08
 */
@Slf4j
@RestController
public class OkController {

    @Resource
    StatisticalImpl statistical;
    @Resource
    UserDao userDao;

    @Autowired
    ChatterService chatterService;

    @Autowired
    ModelService modelService;
    @Resource
    OnlineStatusCheckTask onlineStatusCheckTask;
    @Resource
    UserService userService;
    @Resource
    PushToAppFlyerRetryTask pushToAppFlyerRetryTask;
    @Resource
    AppflyerPullApiTask appflyerPullApiTask;
    @Resource
    RemoveDataTask removeDataTask;
    @Resource
    SendFlashChatMsgTask sendFlashChatMsgTask;

    @Resource
    MsgAPI msgAPI;

    @RequestMapping("/ok")
    public String ok() {
        return "ok-api";
    }

    @RequestMapping("/sendMsgTest")
    public String sendFlashChatMsgTask() {
        sendFlashChatMsgTask.run();
        return "ok-api";
    }


    @RequestMapping("/pullAppsflyerData")
    public String pullAppsflyerData() {
        appflyerPullApiTask.pullAppsflyerData();
        return "ok-api";
    }

    @RequestMapping("/notice/{date}")
    public String notice(@PathVariable("date") String date) {
        JSONObject object= msgAPI.sendBackMessage(AppEnum.MEBOO, date, "You Got New Like！\uD83D\uDC98", "Click to see who this adorable little one is \uD83D\uDC40");
        return object.toJSONString();
    }



    @RequestMapping("/ok22")
    public String ok22() {
        log.info("ok-api");
        return "ok-api";
    }

    @Resource
    IssueUserPondTask issueUserPondTask;
    @RequestMapping("/userPoolTest")
    public String userPoolTest() {
        issueUserPondTask.execute();
        return "ok-api";
    }

    @RequestMapping("/removeDataTask")
    public String removeDataTask() {
        removeDataTask.run();
        return "ok";
    }

    @RequestMapping("/statistical")
    public String statistical() {
        final UserTaskJobEntity entity = new UserTaskJobEntity();
        entity.setTaskType(TaskTypeEnum.STATISTICAL.getCode());
        entity.setId(3L);
        try {
            final ExecuteResult result = statistical.doExecute(entity);
            return JSONObject.toJSONString(result);

        } catch (Exception e) {
            log.error("pushDateError", e);
        }
        return "statistical";
    }



    @RequestMapping("/pushToAppsflyer")
    public String ok2() {
        String body = "{\"eventValue\":{\"fb_mobile_tutorial_completion\":3},\"appsflyer_id\":\"RVdnrbmFm9R2fusE3uUidc\",\"eventName\":\"Active_Chat\",\"eventCurrency\":\"USD\"}";
        String appId = "com.kly.meboo";
        String auth = "RVdnrbmFm9R2fusE3uUidc";

        Map<String, String> header = Maps.newConcurrentMap();
        header.put("accept", "application/json");
        header.put("content-type", "application/json");
        header.put("authentication", auth);

        final String s = HttpUtil.postPageBodyByHttps("https://api2.appsflyer.com/inappevent/"+appId, header, body);
        System.out.println(s);
        return s;

    }

//    @RequestMapping("/cmToOffline")
//    public String cmOnlineToOffLine() {
//        onlineStatusCheckTask.cmOnlineToOffLine();
//        return "ok";
//    }



    @Resource
    MessageStatusStatisticalTask messageStatusStatisticalTask;
    @RequestMapping("/sumUserMsgReport")
    public String onlineStatusStatisticalTask() {
        messageStatusStatisticalTask.sumUserMsgReport();
        return "ok";
    }

    @Resource
    ChatterUserService chatterUserService;
    @RequestMapping("/test04/{date}/{chatterUserCode}")
    public String onlineStatusStatisticalTask(@PathVariable("date") Integer date, @PathVariable("chatterUserCode") String chatterUserCode) {
        final Response<ChatterDashboardDto> chatterDashboardInfo = chatterUserService.getChatterDashboardInfo(date, chatterUserCode);
        return JSONObject.toJSONString(chatterDashboardInfo);
    }

    @Resource
    PushMsgService pushMsgService;
    @RequestMapping("/test05/{userCode}")
    public String pushMsgService(@PathVariable("userCode") String userCode) {
        final PushMsgEvent pushEvent = PushMsgEvent.builder().app(AppEnum.MECHAT).fromUserCode(userCode).pushBizCodeEnum(PushBizCodeEnum.M20004).build();

        pushMsgService.pushMsg(pushEvent);
        return JSONObject.toJSONString(pushMsgService.pushMsg(pushEvent));
    }


    @Resource
    WlmService wlmService;
    @RequestMapping("/addWlmUser/{userCode}/{userCodeFriend}/{likeType}")
    public String addWlmUser(@PathVariable("userCode") String userCode, @PathVariable("userCodeFriend") String userCodeFriend, @PathVariable("likeType") Integer likeType) {

        AddWlmReq req = new AddWlmReq();
        req.setUserCodeFriend(userCodeFriend);
        req.setUserCode(userCode);
        req.setLikeType(likeType);

        final Response<AddWlmUserDto> addWlmUserDtoResponse = wlmService.addWlmUser(req);
        return JSONObject.toJSONString(addWlmUserDtoResponse);
    }

    @RequestMapping("/wlmList/{userCode}")
    public String wlmList(@PathVariable("userCode") String userCode) {

        final List<WlmUserDto> wlmUserDtoList = wlmService.wlmList(userCode, new Page(500, 1)).getData();
        final Integer wlmNum = wlmService.wlmNum(userCode).getData();

        return JSONObject.toJSONString(wlmNum + "----" + wlmUserDtoList);
    }

    @RequestMapping("/user/online/{modelUserCode}/{userCode}")
    public String userOnline(@PathVariable("modelUserCode") String modelUserCode, @PathVariable("userCode") String userCode) {

        AddWlmReq addWlmReq = new AddWlmReq();
        addWlmReq.setLikeType(LikeEnum.LIKE_EACH_OTHER.getType());
        addWlmReq.setUserCode(modelUserCode);
        addWlmReq.setUserCodeFriend(userCode);
        addWlmReq.setFlashchatFlag(Boolean.TRUE);

        final Response<AddWlmUserDto> addWlmUserDtoResponse = wlmService.connectByFlashChat(addWlmReq);
        log.info("userOnline, userCode:{}, result:{}", userCode, JSONObject.toJSONString(addWlmUserDtoResponse));
        return userCode + "--" + JSONObject.toJSONString(addWlmUserDtoResponse);
    }


    @Resource
    UserTaskDetailDao userTaskDetailDao;
    @RequestMapping("/pushaf/{taskId}")
    public String pushToAppFlyerRetryTask(@PathVariable("taskId") Long taskId) {
        final UserTaskDetailEntity userTaskDetailEntity = userTaskDetailDao.selectByPrimaryKey(taskId);
        final String s = pushToAppFlyerRetryTask.retryPushToAf(userTaskDetailEntity);
        return s;
    }



}
